package com.itzhoun.createModel.singleton;

/**
 * @author 周楠
 * @Date 2022/12/08/下午 10:56
 */
public class SingletonTest {

    public static void main(String[] args) {
        //定义卖票 一共10张票
        System.out.println("懒汉式 卖票多线程测试");
        Runnable runnable = () -> {
            SingletonLazy.getInstance().setTicket();
            System.out.println("懒汉式卖了一张票,还剩"+ SingletonLazy.getInstance().ticket);
        };
        Thread t1 = new Thread(runnable);
        Thread t2 = new Thread(runnable);

        t1.start();
        t2.start();

        System.out.println("饿汉式,观测是否线程安全");
        Runnable runnable2 = () -> {
            SingletonAngry.getInstance().setTicket();
            System.out.println("饿汉式卖了一张票,还剩"+ SingletonAngry.getInstance().ticket);
        };
        //先卖一张
        Thread t3 = new Thread(runnable2);
        t3.start();

        try {
            //在卖一张
            Thread.sleep(3000);
            Thread t4 = new Thread(runnable2);
            t4.start();

        }catch (InterruptedException e){
            e.printStackTrace();
        }





        //结果：
        //懒汉式 卖票多线程测试
        //卖了一张票,还剩9
        //卖了一张票,还剩9
        //会获取到同一个对象 线程不安全

        //饿汉式
        //得到了八张票  线程安全
    }
}
